<?php


/**
 * this class provides functions for set or get an application module from the session
 * @author		ungerboeck simon <ungerboeck_simon@hotmail.com>
 *
 */
class Module {

    /**
     * sets the module id in the session
     * @param         string         $moduleName
     * @throws        Exception      
     */
    public static function setModule($moduleName) {
        $applicationModule = ApplicationModule::where('name', $moduleName)->first();
        if ($applicationModule != null)
            Session::put('module', $applicationModule->id);
        else
            throw new Exception('not able to set module in session: module id "' .$moduleId. '" doesnt exist in db');
    }


    /**
     * returns the application module which could be found in the session
     * @return         mixed        $applicationModule
     */
    public static function getModule() {
        $applicationModule = null;
        if (Session::has('module')) {
            $moduleId = intval(Session::get('module'));
            $applicationModule = ApplicationModule::find($moduleId);
        }
        return $applicationModule;
    }
    
    
    /**
     * returns the ajax url for module in session
     * @param 		string		 $baseUrl
     * @return 		string
     */
    public static function getAjaxUrl($baseUrl = PORTAL_URL){
        return Helper::addUrlSlash($baseUrl) . strtolower(Module::getModule()->getAjaxName()) . '/ajax';
    }
}


?>